package test_gui04;

import javax.swing.event.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.*; 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import java.io.*;
import java.net.*;


public class Main extends JFrame //implements ActionListener
{
	
	private JLabel etat, message, lengthL, widthL, areaL;
	private JTextField lengthTF, widthTF, areaTF;
	private JButton calculateB, exitB;
	private JButton IO1;
	
	public static BufferedWriter os = null;
	public static BufferedReader is = null;
	
	public static Socket clientSocket = null;	
	
	public Main() 
	{
	    setTitle("Appli Client - 2016");
	    setSize( 500, 500 );

	 // icon de l'appli
	    Image img = Toolkit.getDefaultToolkit().getImage("d:/client.png");
	    setIconImage(img);

		getContentPane().setLayout(new BorderLayout()); 
		StatusBar statusBar = new StatusBar(); 
		
		Container pane = getContentPane();
		
		//pane.setLayout(null); // disable layout manager - use absolute layout

		
	
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		/*
		addWindowListener(new WindowAdapter() { 
		
			public void windowClosing(WindowEvent event) 
			{
					System.exit(0); 
			}
		}); 

		*/
		
			
		etat = new JLabel("dfdsfdfds");
		message = new JLabel("rgregrh");
		
		//pane.add(etat);
		//pane.add(message);
		
		//this.setLocationRelativeTo(null);
	    //On dfinit le layout  utiliser sur le content pane
	    //Trois lignes sur deux colonnes
	   // this.setLayout(new GridLayout(5, 2, 10, 10));
	    //On ajoute le bouton au content pane de la JFrame
	    this.getContentPane().add(new JButton("1"));
	    //this.getContentPane().add(new JButton("2"));
	    //this.getContentPane().add(new JButton("3"));
	    //this.getContentPane().add(new JButton("4"));
	    this.getContentPane().add(new JButton("5"));

	    
	    
	    pane.setLayout(null);
	    
	    String text = "A JTextArea object represents a multiline area for displaying text. "
	            + "You can change the number of lines that can be displayed at a time, "
	            + "as well as the number of columns. You can wrap lines and words too. "
	            + "You can also put your JTextArea in a JScrollPane to make it scrollable.";
	    
	    JTextArea textArea2 = new JTextArea(text, 5, 10);
	    textArea2.setPreferredSize(new Dimension(100, 100)); 
	    JScrollPane scrollPane = new JScrollPane(textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	    textArea2.setLineWrap(true);

	    pane.add(scrollPane);
	    scrollPane.setBounds(150 , 330, 250, 50);	

	    
	    JButton btn_connexion = new JButton("Connexion");
	    btn_connexion.addActionListener(new action_btn_connexion()); 
	    
	    JButton btn_retourne_datas = new JButton("Retourne datas");
	    btn_retourne_datas.addActionListener(new action_btn_retourne_datas()); 
	    
	    JButton btn_cmd1 = new JButton("Cmd 1");
	    btn_cmd1.addActionListener(new action_btn_cmd1()); 

	    MyButtonListener listener = new MyButtonListener();
	    
	    JButton btn_ctrl1 = new JButton("IO1");
	    //btn_ctrl1.addActionListener(new action_btn_ctrl1()); 
	    btn_ctrl1.addActionListener(listener);

	    
	    JButton btn_ctrl2 = new JButton("IO2");
	    btn_ctrl2.addActionListener(listener);
	    
	    JButton btn_ctrl3 = new JButton("IO3");
	    btn_ctrl3.addActionListener(listener);
	    
	    JButton btn_ctrl4 = new JButton("IO4");
	    btn_ctrl4.addActionListener(listener);
	    
	    
	    ImageIcon imageIcon1 = new ImageIcon("d:/red_32x32.png");
	    JLabel label1 = new JLabel(imageIcon1);
	    ImageIcon imageIcon2 = new ImageIcon("d:/red_32x32.png");
	    JLabel label2 = new JLabel(imageIcon2);
	    ImageIcon imageIcon3 = new ImageIcon("d:/red_32x32.png");
	    JLabel label3 = new JLabel(imageIcon1);
	    ImageIcon imageIcon4 = new ImageIcon("d:/red_32x32.png");
	    JLabel label4 = new JLabel(imageIcon2);


	    
	    
	    btn_connexion.setBounds(25 , 20, 150 , 50);
	    btn_retourne_datas.setBounds(25 , 80, 150 , 50);
	    btn_cmd1.setBounds(25 , 140, 150 , 50);
	    
	    btn_ctrl1.setBounds(25 , 250, 60 , 50);
	    btn_ctrl2.setBounds(100 , 250, 60 , 50);
	    btn_ctrl3.setBounds(175 , 250, 60 , 50);
	    btn_ctrl4.setBounds(250 , 250, 60 , 50);
	    
	    label1.setBounds(25 , 200, 60 , 50);
	    label2.setBounds(100 , 200, 60 , 50);
	    label3.setBounds(175 , 200, 60 , 50);
	    label4.setBounds(250 , 200, 60 , 50);
		    
	    //http://icons.vrx.net/parts/buttons/green/
	    
	    pane.add(btn_connexion);
	    pane.add(btn_retourne_datas);
	    pane.add(btn_cmd1);

	    pane.add(btn_ctrl1);
	    pane.add(btn_ctrl2);
	    pane.add(btn_ctrl3);
	    pane.add(btn_ctrl4);
	    pane.add(label1);
	    pane.add(label2);
	    pane.add(label3);
	    pane.add(label4);
	    
	    
	    
	    JButton b4 = new JButton("4");
	    JButton b5 = new JButton("5");
	    JLabel  lbl = new JLabel("5");

	    
	    lbl.setText("connard");
	    
	    pane.add(b4);
	    pane.add(b5);
	    pane.add(lbl);
	    
	    
	    Insets insets = pane.getInsets();
	    Dimension size = btn_connexion.getPreferredSize();

	    b4.setBounds(50 , 300, 50, 14); // x, y, largeur, hauteur
	    b5.setBounds(50 , 350, 50, 14);

	    lbl.setBounds(50 , 250, 50, 14);	
	  
	    
	    
	    this.setLayout(new BorderLayout());
	    //getContentPane().add(BorderLayout.SOUTH, statusBar); 
		pane.add(BorderLayout.SOUTH, statusBar); 

		
	} 
	
	public class MyButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String actionCommand = e.getActionCommand();
            System.out.println(actionCommand);
            
            if(actionCommand == "IO1")
            {
            	System.out.println("sdff");
                
            }
            
            if(actionCommand == "IO2")
            {
            	System.out.println("Deux");
                
            }
            
            if(e.getSource() == calculateB) 
            {
            	
            }
            
        }
    }
	
	public static void tcp() 
	{ 
	     System.out.println("TRACE");
	
	}
	
	
	public static void main(String[] args) 
	{ 
		(new Main()).show(); 
	}
} 



class send_commande 
{
	 Socket socketOfClient;

		void lulu(String cmd)
		{
			 String serverHost = "localhost";
			
			 try
			 {
				socketOfClient = new Socket(serverHost, 8081);
				// envoi une commande au serveur
				PrintWriter pw = new PrintWriter(socketOfClient.getOutputStream(), true);
				pw.println(cmd);
				//socketOfClient.close();
				System.out.println("***** " + cmd);
			} 
			catch (IOException e) 
			{
				System.out.println(e);
			}
			
		}
		void receive(String msg)  
		  {
		  	try {
			   byte[] mybytearray = new byte[1024];
			    InputStream is = socketOfClient.getInputStream();
			    FileOutputStream fos = new FileOutputStream("d:/xs.txt");
			    BufferedOutputStream bos = new BufferedOutputStream(fos);
			    int bytesRead = is.read(mybytearray, 0, mybytearray.length);
			    bos.write(mybytearray, 0, bytesRead);
			    
			    System.out.println("Reception du fichier"+msg);
			    
			   bos.close();
			    socketOfClient.close();
		  	}
		  	catch (IOException e) 
			{
				System.out.println(e);
			}

		  }

}

class toto 
{
	
	public toto() 
	{

			String serverHost = "localhost";
			 
		    Socket socketOfClient;
		    try {
					socketOfClient = new Socket(serverHost, 8081);
					 
					 // envoi un message au serveur
					 BufferedReader br = new BufferedReader(new InputStreamReader(socketOfClient.getInputStream()));
					 PrintWriter pw = new PrintWriter(socketOfClient.getOutputStream(), true);
					 pw.println("Salut Serveur");
					
					 // voici la rponse du serveur
					 InputStream s1In = socketOfClient.getInputStream();
					 DataInputStream dis = new DataInputStream(s1In);
					 String st = new String (dis.readUTF());
					 System.out.println(st);
					 // When done, just close the connection and exit
					 dis.close();
					 s1In.close();
					 socketOfClient.close();
			} 
		    
		    catch (UnknownHostException e)
		    {
				// TODO Auto-generated catch block
				e.printStackTrace();
                System.out.println(e.toString());
			} 
		    catch (IOException e) 
		    {
				// TODO Auto-generated catch block
				e.printStackTrace();
                System.out.println(e.toString());
			} 

	}
    
}

class action_btn_connexion implements ActionListener 
{
	public void actionPerformed(ActionEvent e)
	{
		//int mc = JOptionPane.WARNING_MESSAGE;
		//String str = JOptionPane.showInputDialog (null, "Enter text:", "Title", mc);
		
		JOptionPane.showMessageDialog(null, "Attention" );
		
		toto tcp = new toto(); 
		//		aaa.tcp();
		//		System.exit(0);
	}
}


class action_btn_retourne_datas implements ActionListener
{
	public void actionPerformed(ActionEvent e) 
	{
		
		//int mc = JOptionPane.WARNING_MESSAGE;
		//String str = JOptionPane.showInputDialog (null, "Enter text:", "Title", mc);
		
		//JOptionPane.showMessageDialog(null, "Retour du fichier" );
		send_commande b ;         // dfinition de la variable rfrence
		b = new send_commande(); // cration de linstance
		b.lulu("echange_fichier");
	//	b.lulu("echange_fichier");
	
		 b.receive("pipo"); 
	}
}


class action_btn_cmd1 implements ActionListener
{
	public void actionPerformed(ActionEvent e) 
	{
		
		//int mc = JOptionPane.WARNING_MESSAGE;
		//String str = JOptionPane.showInputDialog (null, "Enter text:", "Title", mc);
		
		//JOptionPane.showMessageDialog(null, "Bouton Cmd1" );
		send_commande a ;         // dfinition de la variable rfrence
		a = new send_commande(); // cration de linstance
		a.lulu("cmd_start");
		
	}
}


class action_btn_ctrl1 implements ActionListener
{
	public void actionPerformed(ActionEvent e) 
	{
		
		JOptionPane.showMessageDialog(null, "CTRL1" );
		
		 Object  source=e.getSource();
		
		 
         System.out.println(source);
		 
		/* if (e.getSource() == btn_ctrl1) 
		 {
		 
		//label1
		/*if(e.getSource().equals(b))
        {
            this.l.setText("Testing");
        }*/
		
	}
}



class StatusBar extends JPanel implements ActionListener
{
	private JLabel statusLabel;

	public StatusBar()
	{
		SimpleDateFormat sdf = new SimpleDateFormat( "dd MMM yyyy hh:mm:ss" );
		String hhh =  sdf.format( new GregorianCalendar().getTime() ) ;
		
		setLayout(new BorderLayout(3, 3)); 
		statusLabel = new JLabel(hhh); 
		statusLabel.setBorder(BorderFactory.createLoweredBevelBorder()); 
		statusLabel.setForeground(Color.black);
		add(BorderLayout.CENTER, statusLabel); 
		
		JLabel dummyLabel1 = new JLabel(" dssqdgg "); 
		dummyLabel1.setBorder(BorderFactory.createLoweredBevelBorder()); 
		add(BorderLayout.EAST, dummyLabel1); 

//		JLabel Label2 = new JLabel(" hrqzq "); 
//		Label2.setBorder(BorderFactory.createLoweredBevelBorder()); 
//		add(BorderLayout.WEST, Label2); 

		 ImageIcon imageIcon = new ImageIcon("d:/server.png");
		 JLabel Label2 = new JLabel(imageIcon);
		 Label2.setBorder(BorderFactory.createLoweredBevelBorder()); 
		 add(BorderLayout.WEST, Label2); 

		
		   Timer t = new Timer(1000, this);
		    t.start( );
	} 
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		// TODO Auto-generated method stub
		  SimpleDateFormat sdf = new SimpleDateFormat( "dd MMM yyyy hh:mm:ss" );
		  String hhh =  sdf.format( new GregorianCalendar().getTime() ) ;
		  statusLabel.setText(hhh);
	}
}
